import 'package:flutter/material.dart';
import 'package:flutter_09_game/config/app_color.dart';
import 'package:flutter_09_game/utils/loading/loading_animation.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';

class LoadingUtils {
  static setup() {
    EasyLoading.instance
      ..indicatorType = EasyLoadingIndicatorType.fadingCircle
      ..maskType = EasyLoadingMaskType.none
      ..toastPosition = EasyLoadingToastPosition.center
      ..indicatorSize = 30.0
      ..radius = 16.0
      ..progressColor = Colors.white
      ..backgroundColor = AppColor.textPrimary.withOpacity(0.94)
      ..indicatorColor = Colors.white
      ..textColor = AppColor.color0F1114
      ..userInteractions = false
      ..contentPadding = const EdgeInsets.all(20)
      // ..textPadding = const EdgeInsets.all(20)
      ..textStyle = const TextStyle(
        color: AppColor.color0F1114,
        fontSize: 12,
        fontWeight: FontWeight.w500,
      )
      ..textPadding = const EdgeInsets.symmetric(horizontal: 24, vertical: 12)
      ..loadingStyle = EasyLoadingStyle.custom
      ..dismissOnTap = false;
    EasyLoading.addStatusCallback((status) {
      if (status == EasyLoadingStatus.dismiss && isShowLoading) {
        isShowLoading = false;
      }
    });
  }

  static bool isShowLoading = false;

  static void showLoading({
    String? status,
    Widget? indicator,
    EasyLoadingMaskType? maskType,
    bool? dismissOnTap,
  }) {
    EasyLoading.instance
      ..contentPadding = const EdgeInsets.all(20)
      ..userInteractions = false;

    if (isShowLoading && indicator == null) {
      return;
    }
    indicator ??= const LoadingAnimation(
      color: AppColor.primary,
    );
    isShowLoading = true;
    EasyLoading.show(
      status: status,
      indicator: indicator,
      maskType: maskType,
      dismissOnTap: dismissOnTap,
    );
  }

  static showToast(String? msg) {
    if (msg != null && msg.isNotEmpty) {
      EasyLoading.instance
        ..contentPadding =
            const EdgeInsets.symmetric(horizontal: 16, vertical: 12)
        ..userInteractions = true;
      EasyLoading.showToast(msg);
    }
  }

  static Future<void> dismiss({
    bool animation = true,
  }) {
    return EasyLoading.dismiss(animation: animation);
  }

  static bool get isShow => EasyLoading.isShow;
}
